
#include "debug.h"
#include <sys/types.h>
#include <stdlib.h>


#if 0
#define str_utils_log(msg, ...)	custom_log("UTILS", msg, ##__VA_ARGS__)
#else
#define str_utils_log(msg, ...)
#endif

char * str2upper(char * str, uint32 maxLen)
{
	uint32 i = 0;
	int str_len = 0;

	if(str == NULL)
		return NULL;

	str_len = strlen(str);

	if(str_len == 0 )	// string length shouldn't be 0
		return FALSE;

	maxLen = maxLen < str_len ? maxLen : str_len;

	for(; str[i] != '\0' && i < maxLen; i++)
	{
		if(str[i] >= 'a' && str[i] <= 'z')
			str[i] = str[i] - 32;
	}

	return str;
}

char * str2lower(char * str, uint32 maxLen)
{
	uint32 i = 0;
	int str_len = 0;

	if(str == NULL)
		return NULL;

	str_len = strlen(str);

	if(str_len == 0 )	// string length shouldn't be 0
		return FALSE;

	maxLen = maxLen < str_len ? maxLen : str_len;

	for(; str[i] != '\0' && i < maxLen; i++)
	{
		if(str[i] >= 'A' && str[i] <= 'Z')
			str[i] = str[i] + 32;
	}

	return str;
}

bool str2int32(char * str, uint32 maxLen, int * intgr_ptr)
{
	uint32 i = 0;
	int intgr = 0;
	int coefficient = 1;
	int str_len = 0;
	int num_of_digits = 10;

	if(str == NULL || intgr_ptr == NULL)
		return FALSE;

	str_len = strlen(str);

	if(str_len == 0 )		// string length shouldn't be 0
		return FALSE;

	maxLen = maxLen < str_len ? maxLen : str_len;

	if(str[0] == '-')
	{
		if(str_len == 1)
			return FALSE;

		coefficient = -1;
		i = 1;
		num_of_digits = 11;
		maxLen += 1;
	}

	for(; i < maxLen && i < num_of_digits; i++)
	{
		if(!(str[i] >= '0' && str[i] <= '9'))
			return FALSE;
		
		str_utils_log("i = %d", i);

		intgr = intgr * 10 + str[i] - '0';

		str_utils_log("intgr = %d", intgr);
	}

	* intgr_ptr = intgr * coefficient;
	return TRUE;
}

bool hexstr2int32(char * str, uint32 maxLen, int * intgr_ptr)
{
	uint32 i = 0;
	int n = 0;
	int intgr = 0;
	int str_len = 0;
	int num_of_digits = 8;

	if(str == NULL || intgr_ptr == NULL)
		return FALSE;

	str_len = strlen(str);

	if(str_len == 0 )	// string length shouldn't be 0
		return FALSE;
	
	maxLen = maxLen < str_len ? maxLen : str_len;
	
	for(; i < maxLen && i < num_of_digits; i++)
	{
		if(!((str[i] >= '0' && str[i] <= '9') ||
			(str[i] >= 'A' && str[i] <= 'F') ||
			(str[i] >= 'a' && str[i] <= 'f')))
		{
			return FALSE;
		}
			
		str_utils_log("i = %d", i);

		if(str[i] >= '0' && str[i] <= '9')
			n = str[i] - '0';
		else if(str[i] >= 'A' && str[i] <= 'F')
			n = str[i] - 'A' + 10;
		else
			n = str[i] - 'a' + 10;

		str_utils_log("n = %x", n);

		intgr = intgr * 16 + n;

		str_utils_log("intgr = 0x%x", intgr);
	}

	*intgr_ptr = intgr;
	return TRUE;
}


bool hexstr2int64(char * str, uint32 maxLen, int64 * intgr_ptr)
{
	uint32 i = 0;
	int n = 0;
	int64 intgr = 0;
	int str_len = 0;
	int num_of_digits = 16;

	if(str == NULL || intgr_ptr == NULL)
		return FALSE;

	str_len = strlen(str);

	if(str_len == 0 )	// string length shouldn't be 0
		return FALSE;
	
	maxLen = maxLen < str_len ? maxLen : str_len;
	
	for(; i < maxLen && i < num_of_digits; i++)
	{
		if(!((str[i] >= '0' && str[i] <= '9') ||
			(str[i] >= 'A' && str[i] <= 'F') ||
			(str[i] >= 'a' && str[i] <= 'f')))
		{
			return FALSE;
		}
			
		str_utils_log("i = %d", i);

		if(str[i] >= '0' && str[i] <= '9')
			n = str[i] - '0';
		else if(str[i] >= 'A' && str[i] <= 'F')
			n = str[i] - 'A' + 10;
		else
			n = str[i] - 'a' + 10;

		str_utils_log("n = %x", n);

		intgr = intgr * 16 + n;

		str_utils_log("intgr = 0x%llx", intgr);
	}

	*intgr_ptr = intgr;
	return TRUE;
}

char * data2CString( const uint8_t *inBuf, size_t inBufLen )
{
    char* cString = NULL;
    require_quiet(inBuf, error);
    require_quiet(inBufLen > 0, error);

    cString = (char*) malloc( inBufLen + 1 );
    require(cString, error);
    memcpy( cString, inBuf, inBufLen );
    *(cString + inBufLen ) = '\0';
    return cString;

error:
    if ( cString ) free( cString );
    return NULL;
}
